﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarInfoDBAccess
{
    public class OperationForWS
    {
        private static OperationForWS ofw;

        public static OperationForWS Instance
        {
            get
            {
                if (ofw == null)
                    ofw = new OperationForWS();
                return ofw;
            }
        }

        private OperationForWS()
        { }


        /// <summary>
        /// 从数据库里读取所有的信息
        /// </summary>
        /// <returns></returns>
        public ModelForWS.Brand[] GetAllInfoFormDB_ForWS()
        {
            DBAccess.DBAccessDataContext ddc = new DBAccess.DBAccessDataContext();

            var vcblist = from cb in ddc.CSBrands
                          where cb.IsDelete == 0
                          select cb;
            List<ModelForWS.Brand> cblist = new List<ModelForWS.Brand>();
            foreach (var item in vcblist)
            {
                ModelForWS.Brand brand = new ModelForWS.Brand();
                brand.Id = item.Id;
                brand.Name = item.Name;
                brand.IsDelete = item.IsDelete;
                if (brand.CTList == null)
                    brand.CTList = new List<ModelForWS.CarType>();

                //读取车型
                var vctlist = from ct in ddc.CSCarTypes
                              where ct.BrandID == brand.Id && ct.IsDelete == 0
                              select ct;
                foreach (var cartype in vctlist)
                {

                    ModelForWS.CarType ct = new ModelForWS.CarType();
                    ct.Id = cartype.Id;
                    ct.Name = cartype.Name;
                    ct.IsDelete = cartype.IsDelete;
                    ct.BrandID = brand.Id;
                    ct.CIList = new List<ModelForWS.CarInfo>();

                    //读取车款
                    var vcilist = from ci in ddc.CSCarInfos
                                  where ci.CarTypeId == ct.Id && ci.IsDelete == 0
                                  select ci;
                    foreach (var carinfo in vcilist)
                    {
                        ModelForWS.CarInfo ci = new ModelForWS.CarInfo();
                        ci.Id = carinfo.Id;
                        ci.Name = carinfo.Name;
                        ci.Type = carinfo.Type;
                        ci.Price = carinfo.Price;
                        ci.IsDelete = carinfo.IsDelete;
                        ci.Img = carinfo.Image.ToArray();
                        ci.CtID = carinfo.CarTypeId;

                        ct.CIList.Add(ci);
                    }

                    brand.CTList.Add(ct);
                }

                cblist.Add(brand);

            }
            return cblist.ToArray();
        }




    }
}
